package xyz.zdsoft.attendance_manage_module.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import xyz.zdsoft.attendance_manage_module.bo.AttendanceInfoBo;
import xyz.zdsoft.attendance_manage_module.mapper.TeacherAttendanceMapper;
import xyz.zdsoft.attendance_manage_module.service.TeacherAttendanceService;
import xyz.zdsoft.attendance_manage_module.utils.NotNull;
import xyz.zdsoft.attendance_manage_module.vo.AttendanceInfoVo;
import xyz.zdsoft.common.exception.BizException;
import xyz.zdsoft.common.utils.JwtUtils;
import xyz.zdsoft.common.utils.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-07-28 周三
 * @time 14:15
 * @description
 */
@Service
public class TeacherAttendanceServiceImpl implements TeacherAttendanceService {

    @Resource
    private TeacherAttendanceMapper teacherAttendanceMapper;

    @Override
    public Result<AttendanceInfoVo> queryStudentAttendance(AttendanceInfoBo attendanceInfoBo, HttpServletRequest request) {

        int teacherId;
        try {
            teacherId = Integer.parseInt(JwtUtils.getMemberIdByJwtToken(request));
        } catch (NullPointerException e) {
            throw new BizException("401", "请登录后操作!");
        }

        Page<AttendanceInfoVo> page = new Page<>(attendanceInfoBo.getPageNumber(), attendanceInfoBo.getPageSize());

        QueryWrapper<AttendanceInfoVo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(NotNull.judge(attendanceInfoBo.getCollegesName()), "colleges_name", attendanceInfoBo.getCollegesName());
        queryWrapper.eq(NotNull.judge(attendanceInfoBo.getMajorName()), "major_name", attendanceInfoBo.getMajorName());
        queryWrapper.eq(NotNull.judge(attendanceInfoBo.getGradeName()), "grade_name", attendanceInfoBo.getGradeName());
        queryWrapper.eq(NotNull.judge(attendanceInfoBo.getCourseName()), "course_name", attendanceInfoBo.getCourseName());
        queryWrapper.eq(NotNull.judge(attendanceInfoBo.getClassName()), "class_name", attendanceInfoBo.getClassName());
        queryWrapper.eq(NotNull.judge(attendanceInfoBo.getUserNo()), "user_no", attendanceInfoBo.getUserNo());
        queryWrapper.like(NotNull.judge(attendanceInfoBo.getStudentName()), "student_name", attendanceInfoBo.getStudentName());
        queryWrapper.eq(NotNull.judge(attendanceInfoBo.getAttendanceType()), "attendance_type", attendanceInfoBo.getAttendanceType());

        queryWrapper.ge(NotNull.judge(attendanceInfoBo.getStartDate()), "project_date", attendanceInfoBo.getStartDate());
        queryWrapper.le(NotNull.judge(attendanceInfoBo.getEndDate()), "project_date", attendanceInfoBo.getEndDate());

//        return ResultBody.success(teacherCourseMapper.queryAllCourse(page, queryWrapper));
//        return Result.ok().data(leaveService.page(page, queryWrapper));

        return Result.ok().data(teacherAttendanceMapper.queryAllCourse(page, queryWrapper, teacherId));
    }
}
